`
Next, we executed ls -l, which simply listed the directories.
The result of the ls command succeeded without any specific
errors, so it was sent to the standard output stream.
You’ll practice working with the standard input stream when we
introduce redirection in “Redirection Operators” on page XX.
Control Operators
Control operators in bash are tokens that perform a control
function. Table 1-5 gives an overview of control operators.
Table 1-5
Bash Control Operators
Operator
Description
&
Sends a command to the background.
&&
Used as a logical AND. The second command in the expression will be
evaluated only if the first command evaluated to true.
( and )
Used for command grouping
;
Used as a list terminator. A command following the terminator will run af-
ter the preceding command has finished, regardless of whether it evalu-
ates to true or not.
;;
Ends a case statement.
|
Redirects the output of a command as input to another command.
||
Used as a logical OR. The second command will run if the first one evalu-
ates to false.
Let’s see some of these control operators in action. The &
operator sends any command to the background. If you have a list of
commands in a shell script, sending the first command to the
background will allow bash to continue to the next line even if the
previous command hasn’t finished its work. Commands that are
long-running are often sent to the background to prevent scripts from
hanging.
#!/bin/bash
# This script will send the sleep command to the background
echo "Sleeping for 10 seconds..."
sleep 10 &
# Creates a file
echo "Creating the file test123"
touch test123
# Delete a file
echo "Deleting the file test123"
rm test123
Black Hat Bash (Early Access) © 2023 by Dolev Farhi and Nick Aleks